using System;
using System.Collections;
using System.Collections.Generic;
using Main.Audio;
using Main.Event;
using UnityEngine;
using UnityEngine.Pool;
using ZSW.Framework;

namespace Main.Pool
{
    public class ObjectPoolManager : ZSWF_InitMonoBehaviour
    {
        public List<GameObject> poolPrefabs;

        public List<ObjectPool<GameObject>> poolEffectList = new List<ObjectPool<GameObject>>();

        public Queue<GameObject> soundQueue = new Queue<GameObject>();

        public override void Init()
        {
            base.Init();
            CreatePool();
            CreateSoundPool();

            MainEventSystem.ParticleEffectEvent += OnParticleEffectEvent;
            MainEventSystem.InitSoundEffect += OnInitSoundEffect;
        }

        private void OnDisable()
        {
            MainEventSystem.ParticleEffectEvent -= OnParticleEffectEvent;
            MainEventSystem.InitSoundEffect -= OnInitSoundEffect;
        }

        private void OnParticleEffectEvent(ParticleEffectType type, Vector3 vector)
        {
            var ObjectPool = type switch
            {
                ParticleEffectType.LeavesFall01 => poolEffectList[0],
                ParticleEffectType.LeavesFall02 => poolEffectList[1],
                ParticleEffectType.Rock => poolEffectList[2],
                ParticleEffectType.ReapableScenery => poolEffectList[3],
                _ => throw new ArgumentOutOfRangeException()
            };
            var obj = ObjectPool.Get().transform;
            obj.position = vector;

        }



        public void CreatePool()
        {
            foreach (var item in poolPrefabs)
            {
                Transform parent = new GameObject(item.name).transform;
                parent.SetParent(transform);

                ObjectPool<GameObject> newPool = new ObjectPool<GameObject>(
                    () => Instantiate(item, parent),
                    e => e.SetActive(true),
                    e => e.SetActive(false),
                    e => Destroy(e));

                poolEffectList.Add(newPool);
            }
        }

        public void CreateSoundPool()
        {
            var parent = new GameObject("SoundPool").transform;
            parent.SetParent(transform);

            for (int i = 0; i < 20; i++)
            {
                GameObject newObj = Instantiate(poolPrefabs[4], parent);
                newObj.SetActive(false);
                soundQueue.Enqueue(newObj);
            }

        }

        private GameObject GetPoolObject()
        {
            if (soundQueue.Count < 2)
            {
                CreateSoundPool();
            }
            return soundQueue.Dequeue();
        }

        private void OnInitSoundEffect(SoundDetails details)
        {
            var obj = GetPoolObject();
            obj.GetComponent<SoundItem>().SetSound(details);
            obj.SetActive(true);

            StartCoroutine(ReleaseRoutine(obj, details));
        }

        private IEnumerator ReleaseRoutine(GameObject obj, SoundDetails details)
        {
            yield return new WaitForSeconds(details.soundClip.length);
            obj.SetActive(false);
            soundQueue.Enqueue(obj);
        }

    }
}

